package com.peenongsaban.freelance.utils;

import java.util.Calendar;
import java.util.Locale;
import java.util.Random;

import com.opensymphony.xwork2.ActionContext;
import com.peenongsaban.freelance.bean.RegisterBean;

public class GenerateUniqueID {
	@SuppressWarnings("deprecation")
	public static synchronized String generateServiceID(){
		Calendar now = Calendar.getInstance(new Locale("th", "th"));
		RegisterBean register = (RegisterBean) ActionContext.getContext().getSession().get("register");
		String serviceId = "S_" + register.getUser().getIdCard();
		serviceId += "_" + randomNumber() + "_" + now.getTime().getDay() + now.getTime().getHours() + now.getTime().getMinutes() + now.getTime().getSeconds();
		return serviceId;
	}
	
	@SuppressWarnings("deprecation")
	public static synchronized String generatePaymentID(){
		Calendar now = Calendar.getInstance(new Locale("th", "th"));
		RegisterBean register = (RegisterBean) ActionContext.getContext().getSession().get("register");
		String paymentId = "PM_" + register.getUser().getIdCard();
		paymentId += "_" + randomNumber() + "_" + now.getTime().getDay() + now.getTime().getHours() + now.getTime().getMinutes() + now.getTime().getSeconds();
		return paymentId;
	}
	
	@SuppressWarnings("deprecation")
	public static synchronized String generatePictureID(){
		Calendar now = Calendar.getInstance(new Locale("th", "th"));
		RegisterBean register = (RegisterBean) ActionContext.getContext().getSession().get("register");
		String pictureId = "P_" + register.getUser().getIdCard();
		pictureId += "_" + randomNumber() + "_" + now.getTime().getDay() + now.getTime().getHours() + now.getTime().getMinutes() + now.getTime().getSeconds();
		return pictureId;
	}
	
	private static synchronized String randomNumber() {
		Random rand = new Random();
		int number_random = rand.nextInt(10000000) + 100000;
		return "" + number_random;
	}
}
